package cn.com.zhangwoo.microstar.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.com.microstar.activity.R;
import cn.com.zhangwoo.microstar.adapter.GalleryAdapter;
import cn.com.zhangwoo.microstar.homepage.activity.CityActivity;
import cn.com.zhangwoo.microstar.inteface.IUtilDB;
import cn.com.zhangwoo.microstar.utildb.CommonToast;
import cn.com.zhangwoo.microstar.viewpager.fragment.HomePageViewPagerFragment;
import cn.com.zhangwoo.microstar.widget.MyGallery;
import cn.com.zhangwoo.microstar.widget.TimeGallery;

/***
 * 首页
 * @author Administrator
 *
 */

public class HomeFragment extends Fragment implements OnClickListener{
	private View view;
	private TextView mLocViewCityName;
	private String cityName;//城市
	private Intent intent;
	private MyGallery myGallery;
	private TimeGallery mGalleryGons;//滚动栏
	private GalleryAdapter mGalleryAdapter;//Gallery适配器
	private LinearLayout ll_focus_indicator_container = null;
	private int preSelImgIndex = 0;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_homepage, container, false);
		
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		view=getView();
		InitFocusIndicatorContainer();
		initView();
	}
	/***
	 * 实例化
	 */
	 private void initView() {
		mLocViewCityName=(TextView) view.findViewById(R.id.homepage_fragment_mycity);
		view.findViewById(R.id.homepage_fragment_mycity).setOnClickListener(this);
		view.findViewById(R.id.homepage_fragment_search_views).setOnClickListener(this);
		myGallery=(MyGallery) view.findViewById(R.id.homepage_gallery);
		mGalleryGons=(TimeGallery)view.findViewById(R.id.homepage_gallery2);
		mGalleryAdapter = new GalleryAdapter(IUtilDB.uu,getActivity());
		ViewPager mViewPager=(ViewPager) view.findViewById(R.id.middle_classification_viewpager);
		mViewPager.setAdapter(new cn.com.zhangwoo.microstar.adapter.CategoryPagerAdapter(getFragmentManager(),new HomePageViewPagerFragment()));
		loadData();
	}
	/***
	 * 加载数据
	 */
	private void loadData() {
		myGallery.setAdapter(mGalleryAdapter);
		myGallery.setOnItemClickListener(galleyClick1);
		myGallery.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int selIndex, long arg3) {
    				//修改上一次选中项的背景
    			    selIndex = (selIndex %IUtilDB._TOPHOMEPAGEIMG.length);
    				ImageView preSelImg = (ImageView) ll_focus_indicator_container.findViewById(preSelImgIndex);
    				preSelImg.setImageDrawable(getActivity().getResources().getDrawable(R.drawable.ic_focus));
    				//修改当前选中项的背景
    				ImageView curSelImg = (ImageView) ll_focus_indicator_container.findViewById(selIndex);
    				curSelImg.setImageDrawable(getActivity().getResources().getDrawable(R.drawable.ic_focus_select));
    				preSelImgIndex = selIndex;
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
		mGalleryGons.setAdapter(mGalleryAdapter);
		myGallery.setOnItemClickListener(galleyClick2);
	}
	 OnItemClickListener galleyClick1=new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			CommonToast.showToast(getActivity(), position+"??", 1);
		}
	};
	
	 OnItemClickListener galleyClick2=new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				CommonToast.showToast(getActivity(), position+"??", 1);
			}
		};	
	
	/**
	 * 加载滚动布局
	 */
	  private void InitFocusIndicatorContainer() {
	    	ll_focus_indicator_container = (LinearLayout)view.findViewById(R.id.ll_focus_indicator_container);	
		  	for (int i = 0; i <IUtilDB._TOPHOMEPAGEIMG.length; i++) {
		  	    ImageView localImageView = new ImageView(getActivity());
		  	    localImageView.setId(i);
		  	    ImageView.ScaleType localScaleType = ImageView.ScaleType.FIT_XY;
		  	    localImageView.setScaleType(localScaleType);
		  	    LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(
		  		    24, 24);
		  	    localImageView.setLayoutParams(localLayoutParams);
		  	    localImageView.setPadding(5, 5, 5, 5);
		  	    localImageView.setImageResource(R.drawable.ic_focus);
		  	    this.ll_focus_indicator_container.addView(localImageView);
		}
	  }
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.homepage_fragment_mycity:
			intent = new Intent(getActivity(), CityActivity.class);
			startActivity(intent);
			break;
		case R.id.homepage_fragment_search_views:
			
			break;
		default:
			break;
		}
	}
	  
	  

}
